package com.activenetwork.util.verification;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.activenetwork.custom.uielement.decorate.IdentifierFactory;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.verification.gson.FailurePoint;
import com.google.common.collect.Lists;

public class checkDropdownOptions implements VerificationAction {
	
	private static Logger logger = LoggerFactory.getLogger(checkDropdownOptions.class);
	private String pageName;
	private String pageField;
	private String caseName;
	private String[] values;
	private WebElement element;
	
	public checkDropdownOptions(String pageName, String pageField, String[] values, String caseName){
		this.pageName = pageName;
		this.pageField = pageField;
		this.caseName = caseName;
		this.element = IdentifierFactory.createWebElement(pageName, pageField);
		this.values = values;
	}
	
	public checkDropdownOptions(WebElement element, String[] values, String caseName){
		this.element = element;
		this.caseName = caseName;
		this.values = values;
	}

	@Override
	public void verify() {
		// Get dropdown actual values
		List<String> actualValues = new ArrayList<String>();
		String dataSource = element.getAttribute("data-source").trim();// ["a","b","c"]
		List<String> vals = Lists.newArrayList(dataSource.substring(1, dataSource.length()-1).split(",")); // get rid of []
		for(String s : vals){ //"a", "b", "c"
			String value = s.substring(1, s.length()-1).trim(); // get rid of ""
			if(value.length() > 0){
				actualValues.add(value);
			}else if(value.length() == 0){
				actualValues.add("DEFAULT");
			}
		}
		// Get dropdown expect values
		List<String> expectValues = Lists.newArrayList(values);
		// compare
		String notInActualValues = "";
		String notInExpectValues = "";
		
		for(String s : expectValues){
			if(!actualValues.contains(s)){
				notInActualValues = notInActualValues + s + ",";
			}
		}
		
		for(String s : actualValues){
			if(!expectValues.contains(s)){
				notInExpectValues = notInExpectValues + s + ",";
			}
		}
		
		if(notInActualValues.length() > 1){
			logger.error(pageName + "-" + pageField + " dropdown, these expect values not found : " + notInActualValues);
			EnvContext.failurePointTL.get().add(new FailurePoint(
					caseName, 
					"Verify event category values", 
					"Expect values:" + notInActualValues + " are not found in the event category dropdown"));
		}
		
		if(notInExpectValues.length() > 1){
			logger.error(pageName + "-" + pageField + " dropdown, these values are not expected: " + notInExpectValues);
			EnvContext.failurePointTL.get().add(new FailurePoint(
					caseName, 
					"Verify event category values", 
					"Actual values:" + notInExpectValues + " are not expected in the event category dropdown"));
		}
		
		
	}

}
